﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyPhongVePhim.DataAccess
{
    public class DataAccessConnection
    {
        private string connectionString;

        public DataAccessConnection()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }

        public bool ExecuteNonquery(string querystring)
        {
            SqlConnection conn = null;
            int irow;
            try
            {
                conn = new SqlConnection(connectionString);
                SqlCommand cmm = new SqlCommand(querystring, conn);
                cmm.CommandType = CommandType.Text;
                cmm.CommandText = querystring;
                conn.Open();
                irow = cmm.ExecuteNonQuery();

            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (conn != null)
                conn.Close();
            }

            return (irow >0);
        }

        public DataTable ExecuteReader(string querystring)
        {
            DataTable dt = null;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connectionString);
                dt = new DataTable();
                SqlCommand comd = new SqlCommand(querystring,conn);
                conn.Open();
                SqlDataReader myDa = comd.ExecuteReader(CommandBehavior.CloseConnection);
                dt.Load(myDa);
            }
            catch (Exception)
            {
                return dt;
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
            return dt;
        }

        public bool ExcecuteScaler(string querystring)
        {
            //will to do
            return true;
        }
    }
}
